﻿using Abp.Dependency;
using Abp.Threading.BackgroundWorkers;
using Abp.Threading.Timers;
using System;

namespace ARchGL.Platform.BackgroundWorker
{
    /// <summary>
    /// 清理锁定账户过期缓存
    /// </summary>
    public class WindowsServiceStatusWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        private static int WindowsServiceStatus = 0; //服务状态：0正常，1警告，2异常
        private static int Switch = 1; //开关：0关，1开
        private static DateTime RequestTime = DateTime.Now; //上次请求时间

        public WindowsServiceStatusWorker(AbpTimer timer) : base(timer)
        {
            //Timer.Period = 5000;
            Timer.Period = 120000; //2分钟一次轮询
        }

        protected override void DoWork()
        {
            var minutes = DateTime.Now.Subtract(RequestTime).TotalMinutes;
            //if (minutes < 2) WindowsServiceStatus = 0; //正常

            if (minutes > 5) WindowsServiceStatus = 1; //警告
            else if (minutes > 10) WindowsServiceStatus = 2; //异常（可能是离线了，也可能是服务没有启动）
        }

        /// <summary>设置服务状态</summary>
        public static void SetStatus()
        {
            RequestTime = DateTime.Now;
            WindowsServiceStatus = 0;
        }

        /// <summary>获取当前服务状态：0正常，1警告，2异常</summary>
        public static int GetStatus()
        {
            return WindowsServiceStatus;
        }

        /// <summary>设置开关状态</summary>
        public static void SetSwitch(int _switch)
        {
            Switch = _switch;
        }

        /// <summary>获取开关状态</summary>
        public static int GetSwitch()
        {
            return Switch;
        }
    }
}
